package com.itmuch.cloud.study.feign;

import com.itmuch.cloud.study.entity.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "microservice-provider-user", fallbackFactory = UserFeignClient.FeignClientFallbackFactory.class)
public interface UserFeignClient {

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User findById(@PathVariable("id") Long id);

    @Component
    class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
        private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public UserFeignClient create(Throwable cause) {
            return (UserFeignClient) (Long id) -> {
                FeignClientFallbackFactory.LOGGER.info("fallback; reason was: ", cause);
                User user = new User();
                user.setId(-1L);
                user.setUsername("默认用户");
                return user;
            };
        }
    }

}
